package com.woniu.smart.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.smart.dto.VisitorAccDTO;
import com.woniu.smart.dto.VisitorDTO;
import com.woniu.smart.entity.Access;
import com.woniu.smart.entity.Car;
import com.woniu.smart.entity.Visitor;
import com.woniu.smart.service.AccessService;
import com.woniu.smart.service.CarService;
import com.woniu.smart.service.VisitorService;
import com.woniu.smart.util.Result;
import com.woniu.smart.vo.SelByNameVisitorVO;
import com.woniu.smart.vo.VistorVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：zhuboyuan
 * @date ：Created in 2022/10/20 23:19
 * @description：c{description}
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping("visitors")
public class VisitorController {
    @Autowired
    private VisitorService visitorService;
    @Autowired
    private AccessService accessService;
    @Autowired
    private CarService carService;


    /**
     * @date: 2022/10/22 14:49
     * @return:
     * @description: 查询所有访客(分页 ， 模糊查询)
     * @Auther:zhuboyuan
     */

    @PostMapping("queryAll")
    public Result queryAll(@RequestBody VisitorDTO visitorDTO) {
        //设置从哪开始读
        visitorDTO.setCurrentPage((visitorDTO.getCurrentPage() - 1) * visitorDTO.getPageSize());
        List<VistorVo> list = visitorService.selectByAll(visitorDTO);
        Long total = visitorService.selectPageNums(visitorDTO);
        return Result.ok().data("visitorList", list).data("total", total);
    }

    @PostMapping("queryAllAccess")
    public Result queryAccess(VisitorDTO visitorDTO) {
        List<VistorVo> list = visitorService.selectAccess(visitorDTO);
//        visitorDTO.setCurrentPage((visitorDTO.getCurrentPage()-1)*visitorDTO.getPageSize());
//        Long count = visitorService.selectPageNums(visitorDTO);
        return Result.ok().data("list", list);
    }

    /**
     * @date: 2022/10/22 14:50
     * @return:
     * @description: 根据id查找访客
     * @Auther:zhuboyuan
     */

    @GetMapping("{id}")
    public Result queryById(@PathVariable("id") Long id) {
        VistorVo vistorVo = visitorService.selectById(id);
        return Result.ok().data("vistorVo", vistorVo);
    }

    /**
     * @date: 2022/10/22 14:50
     * @return:
     * @description: 添加
     * @Auther:zhuboyuan
     */


    @PostMapping("")
    public Result insert(@RequestBody VisitorAccDTO visitorAccDTO) {
        int insert = visitorService.insertNewSelective(visitorAccDTO);
        return Result.ok();
    }

    @PostMapping("selByNameVisitor")
    public Result selByNameVisitor(@RequestBody VisitorAccDTO visitorAccDTO) {
        SelByNameVisitorVO vistorVo = visitorService.selByNameVisitor(visitorAccDTO);
        return Result.ok().data("vistorVo", vistorVo);
    }

    /**
     * @date: 2022/10/22 14:50
     * @return:
     * @description: 删除单个
     * @Auther:zhuboyuan
     */

    @DeleteMapping("{id}")
    @Transactional
    public Result delete(@PathVariable("id") Long id) {
        int i = visitorService.deleteById(id);
        int deleteByPrimaryKey = accessService.deleteByPrimaryKey(id);
        int delete = carService.deleteByPrimaryKey(id);
        return Result.ok();
    }


    /**
     * @date: 2022/10/22 14:50
     * @return:
     * @description: 批量删除
     * @Auther:zhuboyuan
     */

    @PostMapping("deleteBatch")
    @Transactional
    public Result deleteBatch(@RequestBody List<Integer> visitorIds) {
        int i = visitorService.deleteBatch(visitorIds);
        int deleteByIds = accessService.deleteByIds(visitorIds);
        int delete = carService.deleteByIds(visitorIds);
        return Result.ok();
    }

    /**
     * @date: 2022/10/22 14:51
     * @return:
     * @description: 修改
     * @Auther:zhuboyuan
     */

    @PutMapping("")
    public Result update(@RequestBody Visitor visitor) {
        int i = visitorService.updateByPrimaryKeySelective(visitor);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.error();
        }

    }
}
